﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ThreePointDrawParallelogram
{
	public partial class MainGUI : Form
	{
		public MainGUI()
		{
			InitializeComponent();
		}

		private void ok_Click(object sender, EventArgs e)
		{
			string[] coordinateStr = new string[6];
			coordinateStr[0] = point1x.Text;
			coordinateStr[1] = point1y.Text;
			coordinateStr[2] = point2x.Text;
			coordinateStr[3] = point2y.Text;
			coordinateStr[4] = point3x.Text;
			coordinateStr[5] = point3y.Text;
			foreach (string coor in coordinateStr)
			{
				if (coor.Equals(""))
				{
					MessageBox.Show("3个点的坐标必须填写完整！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}
			}
			int[] coordinates = new int[6];
			try
			{
				for (int i = 0; i < coordinateStr.Length; i++)
				{
					int eachCoor = int.Parse(coordinateStr[i]);
					if (eachCoor < 0)
					{
						MessageBox.Show("坐标不能小于零！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
						return;
					}
					coordinates[i] = eachCoor;
				}
			}
			catch (Exception)
			{
				MessageBox.Show("坐标必须是整数！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			// 计算第四个点的坐标
			int endPointX = coordinates[2] + coordinates[4] - coordinates[0];
			int endPointY = coordinates[3] + coordinates[5] - coordinates[1];
			// 组件Refresh可以清空已经画的东西
			showArea.Refresh();
			// 在panel组件上面创建画布
			Graphics graphics = showArea.CreateGraphics();
			graphics.SmoothingMode = SmoothingMode.AntiAlias;
			graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
			// 画线
			Pen pen = new Pen(Color.Brown, 2);
			graphics.DrawLine(pen, coordinates[0] + 1, coordinates[1] + 1, coordinates[2] + 1, coordinates[3] + 1);
			graphics.DrawLine(pen, coordinates[0] + 1, coordinates[1] + 1, coordinates[4] + 1, coordinates[5] + 1);
			graphics.DrawLine(pen, coordinates[2] + 1, coordinates[3] + 1, endPointX + 1, endPointY + 1);
			graphics.DrawLine(pen, coordinates[4] + 1, coordinates[5] + 1, endPointX + 1, endPointY + 1);
			// 画点，这里用半径为2的圆代替点
			Brush brush = new SolidBrush(Color.Blue);
			Rectangle r1 = new Rectangle(coordinates[0], coordinates[1], 4, 4);
			Rectangle r2 = new Rectangle(coordinates[2], coordinates[3], 4, 4);
			Rectangle r3 = new Rectangle(coordinates[4], coordinates[5], 4, 4);
			graphics.FillEllipse(brush, r1);
			graphics.FillEllipse(brush, r2);
			graphics.FillEllipse(brush, r3);
			Brush endBrush = new SolidBrush(Color.Red);
			Rectangle r4 = new Rectangle(endPointX, endPointY, 4, 4);
			graphics.FillEllipse(endBrush, r4);
		}
	}
}
